package net.jeeshop.core.util;

import com.google.gson.JsonObject;

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.filter.HTTPBasicAuthFilter;
import com.sun.jersey.core.util.MultivaluedMapImpl;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ws.rs.core.MediaType;

import org.json.JSONException;
import org.json.JSONObject;
/**
 *
 * @author Administrator
 */
public class PhoneCode {

    private static PhoneCode sPhoneCode;
    private static int code = -1;
	/**
	 * 手机验证码
	 */
    private PhoneCode(){
    }

    public static synchronized PhoneCode getInstance(){
        if (sPhoneCode == null){
            sPhoneCode = new PhoneCode();
        }
        return sPhoneCode;
    }

    public boolean isExistCode(){
        if (code == -1){
            return false;
        } else {
            return true;
        }
    }

    public void cleanCode(){
        code = -1;
    }

    private int makeCode(){
        return (int)((Math.random()*9+1)*100000);
    }

    public int getCode(){
        return code;
    }

    public int sendPhoneCode(String phone){
        System.out.println("开始执行");
        code = makeCode();
        System.out.println("验证码:" + code);
    	String httpResponse =  testSend(phone, code);
        System.out.println("1");
        try {
           JSONObject jsonObj = new JSONObject( httpResponse );
           int error_code = jsonObj.getInt("error");
           String error_msg = jsonObj.getString("msg");
           if(error_code == 0){
               System.out.println("2");
               System.out.println("发送验证码成功.");
           }else{
               System.out.println("3");
               System.out.println("发送验证码失败,code is "+error_code+",msg is "+error_msg);
           }
       } catch (JSONException ex) {
           Logger.getLogger(PhoneCode.class.getName()).log(Level.SEVERE, null, ex);
       }
       httpResponse =  testStatus();
       try {
           JSONObject jsonObj = new JSONObject(httpResponse);
           int error_code = jsonObj.getInt("error");
           if( error_code == 0 ){
               int deposit = jsonObj.getInt("deposit");
           }else{
               String error_msg = jsonObj.getString("msg");
           }
       } catch (JSONException ex) {}
        System.out.println("结束");
        return code;
    }

    private String testSend(String phone, int code){
        // just replace key here
        Client client = Client.create();
        client.addFilter(new HTTPBasicAuthFilter(
            "api","key-ff16dccc9b01de89cc15c21c7d7bca12"));
        WebResource webResource = client.resource(
            "http://sms-api.luosimao.com/v1/send.json");
        MultivaluedMapImpl formData = new MultivaluedMapImpl();
        formData.add("mobile", phone);
        formData.add("message", "验证码："+code+"【铁壳测试】");
        ClientResponse response =  webResource.type( MediaType.APPLICATION_FORM_URLENCODED ).
        post(ClientResponse.class, formData);
        String textEntity = response.getEntity(String.class);
        int status = response.getStatus();
        return textEntity;
    }

    private String testStatus(){
        Client client = Client.create();
        client.addFilter(new HTTPBasicAuthFilter(
            "api","key-ff16dccc9b01de89cc15c21c7d7bca12"));
        WebResource webResource = client.resource( "http://sms-api.luosimao.com/v1/status.json" );
        MultivaluedMapImpl formData = new MultivaluedMapImpl();
        ClientResponse response =  webResource.get( ClientResponse.class );
        String textEntity = response.getEntity(String.class);
        int status = response.getStatus();
        return textEntity;
    }
}
